﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NFlickr.Communication;
using NFlickr.Factory;
using NFlickr.Core.Interface;
using System.Xml.Linq;

namespace NFlickr.Core
{
    public class PhotoSets : RequireSignedServiceBase
    {

        private const string GetList = "flickr.photosets.getList";

        internal PhotoSets(FlickrService service)
        {
            initialize(service);
        }


        internal PhotoSets(FlickrService service, User user)
        {
            initialize(service);
        }


        private void initialize(FlickrService service)
        {
            FlickrService = service;
            Frob = service.Frob;
            Token = service.Token;
        }
        public FlickrService FlickrService
        {
            get;
            internal set;
        }

        public User User
        {
            get;
            internal set;
        }

        public Frob Frob
        {
            get;
            internal set;
        }

        public Token Token
        {
            get;
            internal set;
        }

        public bool CanCreate
        {
            get;
            internal set;
        }

        private IList<PhotoSet> _items = null;
        public IList<PhotoSet> Items
        {
            get
            {
                if (_items == null)
                {
                    if (Refresh())
                    {
                        return _items;
                    }
                    else
                    {
                        return null;
                    }
                }
                return null;
            }
        }

        private string _methodName;
        public override string MethodName
        {
            get { return _methodName; }
        }

        public override string TagName
        {
            get { return "photosets"; }
        }

        public override bool Refresh()
        {

            _methodName = GetList;
            IChannel channel = FlickrChannelFactory.CreateInstance();
            channel.Complete += new EventHandler<SendRequestCompletedEventArgs>(channel_Complete);
            channel.SignatureRequired += new NFlickr.Core.Interface.SignatureRequiredEventHandler(channel_SignatureRequired);


            Params = new Dictionary<string, string>();

            Params.Add(NFlickr.Resource.FlickrService.ApplicationKeyName, ApplicationKey);
            Params.Add(NFlickr.Resource.FlickrService.MethodKeyName, MethodName);
            Params.Add(NFlickr.Resource.FlickrService.FrobKeyName, Frob.Value);
            Params.Add("auth_token", Token.Value);

            IResult result = channel.SendRequest(FlickrServiceUrl, this);

            if (result != null && result.ResultCode == 0)
            {
                return true;
            }
            return false;
        }

        string channel_SignatureRequired(object sender, NFlickr.Core.Interface.SignatureRequiredEventArgs args)
        {
            Params.Add(NFlickr.Resource.FlickrService.SignatureKeyName, args.Singnature);
            return GetQueryString(Params);
        }

        void channel_Complete(object sender, NFlickr.Core.Interface.SendRequestCompletedEventArgs args)
        {

            args.Result = ParseResult(args.Response);
            if (args.Result.ResultCode == 0)
            {
                XElement document = XDocument.Parse(args.Response).Element("rsp");
                XElement setElement = document.Element(TagName);
                if (setElement.Attribute("cancreate").Value == "1")
                {
                    CanCreate = true;
                }
                else
                {
                    CanCreate = false;
                }

                _items = (from photoSet in setElement.Elements("photoset")
                          select new PhotoSet(FlickrService)
                          {
                              ID = photoSet.Attribute("id").Value,
                              PrimaryPhoto = new Photo(FlickrService) { PhotoId = photoSet.Attribute("primary").Value },
                              Description = photoSet.Element("description").Value,
                              Title = photoSet.Element("title").Value

                          }).ToList();


            }

        }
    }
}
